/**
 * 文件管理
 */
module.exports = function ($cookies,$scope, $rootScope, $state, caseBarDetailFileService, httpservice) {
    /*登录用户ID*/
    var userId = kbt.decrypt($cookies.get('cookieUserId'));
    /*获取点击的案件的id*/
    var caseId = $cookies.get('caseAppId');
    /*提交的数据集合*/
    var data;

    /**
     * 初始化
     */
    $scope.init = function () {
        $scope.fileDown();
        $scope.fileUpload();

        /*用于固定导航条*/
        sessionStorage.navNum = 4;
    };

    /**
     * 选择文件
     */
    $(".fileManager-upload").on("click", function () {
        document.getElementById("upload-input").click();
    });

    /**
     * 选择文件
     */
    $('#upload-input').on('change', function () {
        data = new FormData();
        data.append('filename1', this.files[0]);
        data.append("creatorId", userId);
        data.append("caseId", caseId);
        /*是否为文本类型的正则表达式*/
        var checkType = /^.*?\.(doc|DOC|xlsx|XLSX|txt|TXT|docx|DOCX|pdf|PDF|xls|XLS)$/;
        /*如果选中的文件名不为空，则显示文件名*/
        if (this.files[0].name != "" && this.files[0].name != null && this.files[0].name != undefined) {
            var fileName = this.files[0].name;
            var point = fileName.lastIndexOf(".");
            var fileType = fileName.substr(point);
            if(checkType.test(fileType)){
                $("#id-file-name").text(this.files[0].name);
            }else{
                $("#id-file-name").text("不支持所选文件类型，请重新选择");
            }

        }


    });

    /**
     * 上传文件
     */
    $("#submit-file").click(function () {

        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        if (data.get('filename1') != undefined && data.get('filename1') != null) {
            if (data.get('filename1').name == null || data.get('filename1').name == undefined
                                                                || data.get('filename1').name == "") {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                alert("选择文件出错，请重新选择");
            } else {
                caseBarDetailFileService.uploadPhoto(data).then(function (response) {
                    $rootScope.loadingShow = false;
                    $rootScope.fadeShow = false;

                    if (response.code == 1) {
                        $scope.fileUpload();
                        $("#id-file-name").text("未选择任何文件");

                    }else{
                        $rootScope.loadingShow = false;
                        $rootScope.fadeShow = false;
                        alert("抱歉！由于网络原因上传失败，请重试！");
                    }
                }, function (response) {
                    $rootScope.loadingShow = false;
                    $rootScope.fadeShow = false;
                    console.error('uploadPhoto error', response.status, response.statusText);
                });
            }
        } else {
            alert("请选择一个文件");
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
        }

    });


    /**
     * 下载文件列表
     */
    $scope.fileDown = function () {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;

        caseBarDetailFileService.fileDown().then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;

            if (data.code == 1) {
                $scope.files = data.data;
                for(var i=0;i<=$scope.files.length;i++){
                    $scope.files[i].downUrl =httpservice.downDoFileUrl+$scope.files[i].aliasTitle;
                }
            }
        }, function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('fileDown error', response.status, response.statusText);
        });

    };

    /**
     * 文件上传列表
     */
    $scope.fileUpload = function () {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;

        caseBarDetailFileService.fileUpload(caseId).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;

            if (data.code == 1) {
                if(data.data.length==0){
                    $('#file-empty-id').show();
                    $('#file-container-id').hide();
                }else{
                    $('#file-empty-id').hide();
                    $('#file-container-id').show();
                }
                $scope.upfiles = data.data;
            }
        }, function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('fileUpload error', response.status, response.statusText);
        });

    };

    /**
     * 下载上传的文件
     */
    $scope.downUpFile = function () {
        var filename = $('#hide-down-title').val();
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        if (filename != undefined && filename != "") {
            caseBarDetailFileService.downUpFile(filename).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                // if (data.code == 1) {
                    $scope.upfiles = data.data;
                // }
            }, function (response) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                console.error('downUpFile error', response.status, response.statusText);
            });
        } else {
            alert("获取文件失败");
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
        }
    };


    /**
     * 下载上传的文件(链接方式)
     */
    $scope.downUpFile_line = function (aliasTitle) {
        var url = httpservice.downUpFileUrl + aliasTitle;
        window.open(url);
    };


    /**
     * 删除上传的文件
     */
    $scope.delUpFile = function (aliasTitle) {
        var legalInstrumentTitle = $('#id-legalInstrumentTitle').text();
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        if (aliasTitle != undefined && aliasTitle != "") {
            caseBarDetailFileService.delUpFile(aliasTitle,legalInstrumentTitle,userId,caseId).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1) {
                    $scope.fileUpload();
                }
            }, function (response) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                console.error('downUpFile error', response.status, response.statusText);
            });
        } else {
            alert("获取文件失败");
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
        }
    };



    $scope.init();


};
